var Myaddress = decodeURI(window.location.href).split('=');
var MyaddressDetail = Myaddress[4];
var provinceId;
var cityId;
var countyId;
var addressUrl = decodeURI(decodeURI(window.location.href)).split('&');
console.log(addressUrl);
var ifEdit = GetQueryString('addressId');
//是否获取到地区
$(function(){
    if(getItem('save_add_name')==""||getItem('save_add_name')==undefined||getItem('save_add_name')==null){
    }else{
        $('#name').val(getItem('save_add_name'))
        $('#phone').val(getItem('save_add_phone'))
    }
    if(ifEdit==""||ifEdit==undefined||ifEdit==null){
        $('#save').show();
        $('#save2').hide();
        if(MyaddressDetail==undefined||MyaddressDetail==null||MyaddressDetail==""){
            $('.fl').show();
            $('#address').hide();
            provinceId = '';
            cityId = '';
            countyId = '';
        }else{
            $('#address').val(MyaddressDetail);
            $('.fl').hide();
            $('#address').show();
            provinceId = Myaddress[1].split('&')[0];
            cityId = Myaddress[2].split('&')[0];
            countyId = Myaddress[3].split('&')[0];
        }
    }else{
        $('#save').hide();
        $('#save2').show();
        $('.mui-title').html('编辑地址');
        var name = addressUrl[1].split('=')[1];
        var phone = addressUrl[2].split('=')[1];
        var detail = addressUrl[3].split('=')[1];
        var detailBox = addressUrl[4].split('=')[1];
        $('#name').val(name);
        $('#phone').val(phone);
        $('#address').val(detail);
        $('#detail').val(detailBox);
        $('.fl').hide();
        $('#address').show();
        provinceId = GetQueryString('proId');
        cityId = GetQueryString('cityId');
        countyId = GetQueryString('countyId');
    }
})



//所在地区
$('.fl,#address').click(function () {
    if($('#name').val()==""||$('#name').val()==null||$('#name').val()==undefined){
    }else{
        setItem('save_add_name',$('#name').val());
    }
    if($('#phone').val()==""||$('#phone').val()==null||$('#phone').val()==undefined){
    }else{
        setItem('save_add_phone',$('#phone').val());
    }
    window.location.href="address-detail_v2.html"
})

//添加保存
$('#save').click(function () {
    var ifDefault = '';
    if($('#ifDefault').hasClass('mui-active')){
        ifDefault = 1;
    }else{
        ifDefault = 0;
    }
    $.ajax({
        url:'/user/addUserAddress.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            provinceId:provinceId,
            cityId:cityId,
            countyId:countyId,
            ifDefault:ifDefault,
            name:$('#name').val(),
            phone:$('#phone').val(),
            detail:$('#detail').val(),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                mui.alert('保存地址成功',function () {
                    removeItem('save_add_name');
                    removeItem('save_add_phone');
                    history.go(-3);
                    //window.location.href=history.go(-2);
                    //window.location.href="../personal_v2.html";
                });
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
        }
    })
})

//编辑保存
$('#save2').click(function () {
    var ifDefault = '';
    if($('#ifDefault').hasClass('mui-active')){
        ifDefault = 1;
    }else{
        ifDefault = 0;
    }
    $.ajax({
        url:'/user/editUserAddress.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            addressId:ifEdit,
            provinceId:provinceId,
            cityId:cityId,
            countyId:countyId,
            ifDefault:ifDefault,
            name:$('#name').val(),
            phone:$('#phone').val(),
            detail:$('#detail').val(),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                mui.alert('保存地址成功',function () {
                    removeItem('save_add_name');
                    removeItem('save_add_phone');
                    console.lgo(history.go(-4));
                    //window.location.href=history.go(-2);
                    //window.location.href="../personal_v2.html";
                });
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
        }
    })
})